import { _decorator, Component, Label, Node, tween, v3 } from 'cc';
import { PrefsManager } from './PrefsManager';
import { GameUtil } from './GameUtil';
import { WeaponData, weaponProperty, WEAPONTYPE } from './GameConstant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { GameManager } from './GameManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('NewWeaponPanel')
export class NewWeaponPanel extends Component {
    @property(Label)
    WeaponName: Label = null;

    @property(Label)
    CurWeapon: Label[] = [];

    @property(Label)
    NextWeapon: Label[] = [];

    private newWeaponType: WEAPONTYPE = WEAPONTYPE.UZI;

    protected start(): void {
        this.show();
    }

    show() {
        this.newWeaponType = GameUtil.getAdjacentEnumCirculation(WEAPONTYPE, PrefsManager.Instance.userData.haveWeapon[PrefsManager.Instance.userData.haveWeapon.length - 1]);
        const nextName: string = ZTool.GetEnumKeyByValue(WEAPONTYPE, this.newWeaponType);
        this.WeaponName.string = nextName;
        const curName: string = ZTool.GetEnumKeyByValue(WEAPONTYPE, PrefsManager.Instance.userData.CurWeapon);
        const curWeaponData: WeaponData = weaponProperty[curName];
        this.readData(this.CurWeapon, curWeaponData);
        const nextWeaponData: WeaponData = weaponProperty[nextName];
        this.readData(this.NextWeapon, nextWeaponData);
    }

    readData(weapon: Label[], weaponData: WeaponData) {
        weapon[0].string = weaponData.power.toString();
        weapon[1].string = weaponData.rate.toString();
        weapon[2].string = weaponData.range.toString();
    }

    getNewWeapon() {
        PrefsManager.Instance.userData.haveWeapon.push(this.newWeaponType);
        PrefsManager.Instance.userData.CurWeapon = this.newWeaponType;
        PrefsManager.Instance.saveData();
    }

    hide() {
        AudioManager.PlaySound(Audios.Click);
        tween(this.node)
            .to(0.3, { scale: v3(0, 0, 0) }, { easing: `quadOut` })
            .call(() => {
                this.getNewWeapon();
                GameManager.Instance.showSettlePanel();
                this.node.active = false
            })
            .start();
    }

    protected onEnable(): void {
        tween(this.node)
            .to(0.3, { scale: v3(1.2, 1.2, 1.2) }, { easing: `quadIn` })
            .to(0.2, { scale: v3(1, 1, 1) }, { easing: `quadIn` })
            .start();
    }
}


